package com.cloudpivot.receivales;

import com.alibaba.fastjson.JSONObject;
import com.cloudpivot.receivales.excel.domain.ExcelForBJDTO;
import com.cloudpivot.receivales.excel.service.ProcessExcelService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;

@SpringBootTest
class CloudpivotApplicationTest6 {

    @Autowired
    private ProcessExcelService processExcelService;

    @Test
    public void testExcel() {
        try {
            String url = "/Users/kent/Downloads/导入报价模版.xlsx";
            String base64=inputStreamToBase64(url);
            InputStream inputStream = base64ToInputStream(base64);
//            File file = new File("/Users/kent_1/cloudpivot/docmode/Import.xlsx");
//            InputStream inputStream = new FileInputStream(file);
//            Map<String,Object> objects = processExcelService.importExcelForPresale(inputStream);
            Map<String,Object> ret = processExcelService.importExcelForBJ(inputStream);
            System.out.println("---------------"+JSONObject.toJSONString(ret));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void invokeTest() {

        ExcelForBJDTO excelForBJDTO = new ExcelForBJDTO();
        Field[] fields= excelForBJDTO.getClass().getDeclaredFields();
        int index=0;
        try {
            for (Field f : fields) {
                String attrName = f.getName();
                String methodName = attrName.substring(0, 1).toUpperCase() + attrName.substring(1);
                Method setMethod = ExcelForBJDTO.class.getMethod("set" + methodName, String.class);
                setMethod.invoke(excelForBJDTO, String.valueOf(index));
                index++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("-----------" + JSONObject.toJSONString(excelForBJDTO));

    }

    /**
     * inputstream 转 base64
     * @param path
     * @return
     */
    private String inputStreamToBase64(String path){
        try {
            File file = new File(path);
            FileInputStream inputStream = new FileInputStream(file);
            byte[] buffer = new byte[(int) file.length()];
            inputStream.read(buffer);
            inputStream.close();
            Base64.Encoder encoder = Base64.getEncoder();
            String base64 = encoder.encodeToString(buffer);
            return base64;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * base64 转 inputstream
     * @param base64
     * @return
     */
    private InputStream base64ToInputStream(String base64) {
        byte[] bytes = Base64.getDecoder().decode(base64);
        InputStream inputStream = new ByteArrayInputStream(bytes);
        return inputStream;
    }
}
